package com.d4peng.test.jna;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Union;

import java.io.File;

/**
 * Jna:UNION类型
 *
 * @author d4peng
 * @date 2021-11-25
 */
public class JnaDemo4 {

    interface UnionOps extends Library {
        public static class Data extends Union {
            public int i;
            public float f;
            public String str;

            // 引用传递
            public static class ByReference extends Data implements Union.ByReference {
            }

            // 值传递
            public static class ByValue extends Data implements Union.ByValue {
            }
        }

        // 传入person的结构体,返回person的结构体
        Data.ByReference test(Data.ByReference byReference);
    }

    public static void main(String[] args) {
        String path = new File("").getAbsoluteFile().getPath() + "\\src\\main\\java\\com\\d4peng\\test\\jna\\c\\union.dll";
        UnionOps unionOps = (UnionOps) Native.loadLibrary(path, UnionOps.class);
        UnionOps.Data.ByReference byReference = new UnionOps.Data.ByReference();
        byReference.setType(Integer.TYPE);
        byReference.i = 100;

        UnionOps.Data.ByReference data = unionOps.test(byReference);
        System.out.println(data.i);
    }
}
